import PopupTreeInput from '@/components/PopupTreeInput'
import KtTable from '@/components/Core/KtTable'
import KtButton from '@/components/Core/KtButton'
import TableColumnFilterDialog from '@/components/Core/TableColumnFilterDialog'

export let data = {
  components: {
    PopupTreeInput,
    KtTable,
    KtButton,
    TableColumnFilterDialog
  },
  name: 'Menu',
  data () {
    return {
      filters: {
        name: '',
        authorName: ''
      },
      pageRequest: {pageNum: 1, pageSize: 15},
      pageResult: {},
      listMenus: [],
      columns: [],
      filterColumns: [],
      size: 'small',
      dialogVisible: false,
      dataForm: {
        id: 0,
        cnName: '',
        enName: '',
        status: 1,
        unique: ''
      },
    }
  },
  methods: {
    findPage: function (data) {
      if (data != null) {
        this.pageRequest = data.pageRequest
      }
      this.pageRequest.name = this.filters.name
      this.$api.dict.findPageType(this.pageRequest).then((res) => {
        this.pageResult = res.body
        this.listMenus = res.body
      }).then(data != null ? data.callback : '')
    },
    // 处理表格列过滤显示
    initColumns: function () {
      this.columns = [
        {prop: 'id', label: 'ID', minWidth: 70},
        {prop: 'cnName', label: '名称', minWidth: 120},
        {prop: 'enName', label: '类型值', minWidth: 120},
        {prop: 'code', label: '编码', minWidth: 120},
        {prop: 'createTime', label: '创建时间', minWidth: 120, formatter: this.dateFormat},
        {prop: 'unique', label: '唯一码', minWidth: 120}
      ]
      this.filterColumns = this.columns
    },
    handleEdit: function (data) {
      this.dataForm.id = data.row.id
      this.dataForm.cnName = data.row.cnName
      this.dataForm.enName = data.row.enName
      this.dataForm.code = data.row.code
      this.dataForm.unique = data.row.unique
      this.dialogVisible = true
    },
    handleAdd: function (data) {
      this.dataForm.id = 0
      this.dataForm.cnName = ''
      this.dataForm.enName = ''
      this.dataForm.code = ''
      this.dataForm.unique = ''
      this.dialogVisible = true
    },
    handleDelete: function (data) {
      this.$api.dict.batchDelete({unique: data.params.unique}).then(data != null ? data.callback : '')
    },
    handleTreeSelectChange: function (data) {

    },
    // 处理表格列过滤显示
    handleFilterColumns: function (data) {
      this.filterColumns = data.filterColumns
      this.$refs.tableColumnFilterDialog.setDialogVisible(false)
    },
    // 处理表格列过滤显示
    displayFilterColumnsDialog: function () {
      this.$refs.tableColumnFilterDialog.setDialogVisible(true)
    },
    submitForm: function () {
      this.$api.dict.saveType(this.dataForm).then((res) => {
        if (res.isSuccess == true) {
          this.$message.success('保存成功')
          this.dialogVisible = false
          this.findPage()
        } else {
          this.$message.error(res.msg)
        }
      })
    }
  },
  mounted () {
    this.initColumns()
    if (this.listMenus.length == 0) {
      this.$api.dict.findPageType({pageSize: 200}).then((res) => {
        this.listMenus = res.body.content
      })
    }
  }
}
